//
//  StopwatchViewController.m
//  iEasyTime
//
//  Created by User on 7/2/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "StopwatchViewController.h"


@implementation StopwatchViewController

@synthesize stopwatch;

-(void)viewDidLoad{
		
	seconds = 0;
	running = NO; 
	[self runTimer];
}

-(IBAction)reset{
	seconds = 0;
	start = 0;
	[self runTimer];
}


-(IBAction)pause{
	if(running == NO){
		if(start == 0){ start = [NSDate timeIntervalSinceReferenceDate];}
		timer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
		running = YES;
	}else{
		[self runTimer];
		[timer invalidate];
		running = NO;
	}
}


-(void)runTimer{
	now = [NSDate timeIntervalSinceReferenceDate];
	
	if(start != 0){ interval = now - start;}
	else{ interval = 0; }
	
	seconds = (int)interval;
	int hundredths = (interval - seconds)*100;

	NSString *myString = [NSString stringWithFormat:@"%02d:%02d:%02d", seconds/60, seconds%60, hundredths];
	stopwatch.text = myString;
}	

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
